package com.feng.controller;


import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.feng.entity.Photo;
import com.feng.service.IPhotoService;
import com.feng.util.CommonUtils;
import com.feng.util.FileUtils;

@Controller
public class FileController {
	
	
	@Autowired
	private IPhotoService photoService;
	
	private Logger log = Logger.getLogger(FileController.class);

	@ResponseBody
	@RequestMapping("loadPdfData")
	public byte[] loadPdfData(String fileName) {
		byte[] pdfbyte = null;
		if (CommonUtils.isNotEmpty(fileName)) {
			String path = "d:/uploadfile/" + fileName;
			pdfbyte = FileUtils.getByteArray(path);
		}

		return pdfbyte;
	}
	
	@RequestMapping(value="/toFileUpload")
	public String toFileUpload() {
		return "fileUpload";
	}
	
	@RequestMapping(value="uploadFile")
	public String uploadFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
		if(file.isEmpty()) return "0";
		Photo photo = new Photo();
		String photoDesc = request.getParameter("photodesc");
		photo.setPhotodesc(photoDesc);
		try {
			photoService.uploadPhoto(file, photo);
		} catch (Exception e) {
			log.error("upload file error", e);
		}
		return "redirect:lifeLogPicIndex";
	}

}
